package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.hdw.CabinetBox;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.biz.client.ClientBizUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.basic.SystemConfigMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.CabinetBoxMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * 箱体业务
 * 
 * @Date:Created in 2018/7/6 9:11
 * @Author:
 * @Modified By: zjn 2018/7/6 9:11
 */ 
@Service
public class CabinetBoxService {

    @Autowired
    CabinetBoxMapper cabinetBoxMapper;
    @Autowired
    AppConfig config;
    @Autowired
    SystemConfigMapper systemConfigMapper;
    /**
    * 查询可用箱号
    * 
    * @Param: [cabinetId, boxStatus]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/6  9:11
    */ 
    public String findOneAvailableBoxNum(String cabinetId, int boxStatus) {
        Integer chargeStage= null;
        // 如果箱体状态显示满箱
        if(boxStatus==CabinetBox.BoxStatus.FULL.getValue()){
            chargeStage=4;
        }
        // 查询出箱号
        List<String> list = cabinetBoxMapper.findAvailableBoxNum(cabinetId, boxStatus,chargeStage);
        if (list.isEmpty()) {
            return null;
        } else if (list.size() == 1) {
            return list.get(0);
        } else {
            return list.get((int) (list.size() * Math.random()));
        }
    }
    /**
    * 查询
    *
    * @Param: [cabinet, boxNum]
    * @Return: cc.yugu.yhdw.common.domain.hdw.CabinetBox
    * @Author By: zjn 2018/7/6  9:14
    */
    public CabinetBox find(String cabinet, String boxNum) {
        // 根据柜子id和箱号 查询表hdw_cabinet_box
        CabinetBox cabinetBox = cabinetBoxMapper.find(cabinet, boxNum);
        // 设置箱体状态名称
        cabinetBox.setBoxStatusName(CabinetBox.BoxStatus.getName(cabinetBox.getBoxStatus()));
        return cabinetBox;
    }
    /**
    * 分页查询
    * 
    * @Param: [terminalBox]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/6  9:18
    */ 
    public Page findPage(CabinetBox terminalBox) {
        Page<CabinetBox> page = new Page<CabinetBox>();
        List<CabinetBox> list = cabinetBoxMapper.findPageResult(terminalBox);
        page.setTotalItems(list.size());
        page.setResult(list);
        return page;
    }
    /**
    * 插入往柜子里添加电池充电的信息
    * 
    * @Param: [cabinetBox]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/6  9:29
    */ 
    public ExtResult insert(CabinetBox cabinetBox) {
        // 根据柜子id和箱号 查询表hdw_cabinet_box，如果不为null
        if (cabinetBoxMapper.find(cabinetBox.getCabinetId(), cabinetBox.getBoxNum()) != null) {
            return ExtResult.failResult("箱号已存在不能重复添加");
        }
        initBoxData(cabinetBox);
        if (cabinetBoxMapper.insert(cabinetBox) == 0) {
            return ExtResult.failResult("操作失败");
        }
        // 根据柜子id查询
        List<CabinetBox> boxs = cabinetBoxMapper.findList(cabinetBox.getCabinetId());
        // 箱体更新
        ClientBizUtils.noticeBox(config, cabinetBox.getCabinetId(), boxs);
        return ExtResult.successResult();
    }
    /**
    * 批量插入
    * 
    * @Param: [cabinetId, batchBox]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/6  14:06
    */ 
    public ExtResult batchInsert(String cabinetId, String batchBox) {
        if (StringUtils.isEmpty(batchBox)) {
            return ExtResult.failResult("操作失败");
        }
        String[] rows = batchBox.split("\r\n");
        Set<String> stringSet = new HashSet<String>();
        for (String r : rows) {
            stringSet.add(r);
        }
        CabinetBox terminalBox = new CabinetBox();
        terminalBox.setCabinetId(cabinetId);
        initBoxData(terminalBox);
        for (String box : stringSet) {
            if (box != null && box.length() > 1) {
                terminalBox.setBoxNum(box);
                if (cabinetBoxMapper.find(terminalBox.getCabinetId(), terminalBox.getBoxNum()) == null) {
                    cabinetBoxMapper.insert(terminalBox);
                }

            }
        }
        List<CabinetBox> boxs = cabinetBoxMapper.findList(cabinetId);
        ClientBizUtils.noticeBox(config, cabinetId, boxs);
        return ExtResult.successResult();
    }
    /**
    * 更新
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/6  14:06
    */ 
    public ExtResult update(CabinetBox entity) {
        // 更新箱号和启用标识
        if (cabinetBoxMapper.update(entity) == 0) {
            return ExtResult.failResult("操作失败");
        }
        List<CabinetBox> boxs = cabinetBoxMapper.findList(entity.getCabinetId());
        ClientBizUtils.noticeBox(config, entity.getCabinetId(), boxs);
        return ExtResult.successResult();
    }

    /**
    * 清空指定箱号里的电池
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/6  14:16
    */ 
    public ExtResult clean(CabinetBox entity) {
        // 设置箱体状态为空箱
        entity.setBoxStatus(CabinetBox.BoxStatus.EMPTY.getValue());
        // 清除
        if (cabinetBoxMapper.clean(entity) == 0) {
            return ExtResult.failResult("操作失败");
        }
        List<CabinetBox> boxs = cabinetBoxMapper.findList(entity.getCabinetId());
        ClientBizUtils.noticeBox(config, entity.getCabinetId(), boxs);
        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [cabinetId, boxNum]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/6  14:21
    */ 
    public ExtResult delete(String cabinetId, String boxNum) {
        if (cabinetBoxMapper.delete(cabinetId, boxNum) == 0) {
            return ExtResult.failResult("箱子被使用不能删除");
        }
        List<CabinetBox> boxs = cabinetBoxMapper.findList(cabinetId);
        ClientBizUtils.noticeBox(config, cabinetId, boxs);
        return ExtResult.successResult();
    }
    /**
    * 初始化箱体状态
    *
    * @Param: [terminalBox]
    * @Return: void
    * @Author By: zjn 2018/7/6  14:53
    */
    private void initBoxData(CabinetBox terminalBox) {
        // 柜子启用标识设置为 1
        terminalBox.setActiveFlag(ConstEnum.Flag.TRUE.getValue());
        // 设置箱体状态为空箱
        terminalBox.setBoxStatus(CabinetBox.BoxStatus.EMPTY.getValue());
    }
}
